package com.alibaba.citrus.cr.b2b.trade.order.facade.service;

import com.alibaba.citrus.cr.b2b.trade.order.repository.OrderLineQueryServiceRepository;
import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crb2btradecenter.api.orderlinequery.service.OrderLineQueryService;
import com.epoch.app.crb2btradecenter.dto.request.OrderLinePageQueryRequest;
import com.epoch.app.crb2btradecenter.dto.request.QuerySaleOrderLine;
import com.epoch.app.crb2btradecenter.dto.request.QuerySaleOrderLineRequest;
import com.epoch.app.crb2btradecenter.dto.response.OrderLinePageQueryResponse;
import com.epoch.app.crb2btradecenter.model.dto.AmountResponse;
import com.epoch.app.crb2btradecenter.model.dto.CountOrderNumberResponse;
import com.epoch.app.crb2btradecenter.model.dto.OrderLineStatAmountResponse;
import com.epoch.app.crb2btradecenter.model.dto.PageQueryOrderLineESListRequest;
import com.epoch.app.crb2btradecenter.model.dto.SearchOrderLineListRequest;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Primary
@Service("OrderLineQueryService")
public class OrderLineQueryServiceImpl implements OrderLineQueryService {

    @Resource
    private OrderLineQueryServiceRepository orderLineQueryServiceRepository;

    @Override
    @RepositoryInvoker(errorCode = "ORDER-A-001-01-16-004")
    public Result<List<OrderLinePageQueryResponse>> pageQuery(OrderLinePageQueryRequest request) {
        PageResult<List<OrderLinePageQueryResponse>> listPageResult  = orderLineQueryServiceRepository.pageQuery(request);
        return Result.listSuccess(listPageResult.getContent(), listPageResult.getTotal());
    }

    @Override
    @RepositoryInvoker(errorCode = "OTS-05-001-10-16-022")
    public Result<List<QuerySaleOrderLine>> queryScItemInfo(QuerySaleOrderLineRequest request) {
        try {
            List<QuerySaleOrderLine> querySaleOrderLines = orderLineQueryServiceRepository.queryScItemInfo(request);
            return Result.success(querySaleOrderLines);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("OTS-05-001-10-16-022", e.getMessage());
        }
    }

    @Override
    @FacadeInvoker(errorCode = "ORDER-A-001-01-16-004")
    public Result<List<OrderLinePageQueryResponse>> pageQueryES(PageQueryOrderLineESListRequest request) {
        Result<List<OrderLinePageQueryResponse>> listResult = orderLineQueryServiceRepository.pageQueryES(request);
        return  Result.listSuccess(listResult.getResult(),listResult.getTotal());
    }

    @Override
    @FacadeInvoker(errorCode = "ORDER-A-001-01-16-005")
    public Result<CountOrderNumberResponse> countOrderNumber(PageQueryOrderLineESListRequest request) {
        Result<Long> listResult = orderLineQueryServiceRepository.countOrderNumber(request);
        CountOrderNumberResponse countOrderNumberResponse=new CountOrderNumberResponse();
        countOrderNumberResponse.setCountNumber(listResult.getResult());
        return  Result.success(countOrderNumberResponse);
    }
    @Override
    public Result<AmountResponse> sumOrderDeliveryAmount(SearchOrderLineListRequest searchOrderLineListRequest){
        Result<AmountResponse> listResult = orderLineQueryServiceRepository.searchOrderLineList(searchOrderLineListRequest);
        if (listResult != null && listResult.getResult()!=null){
            return Result.success(listResult.getResult());
        }
        return Result.success(null);
    }

    @Override
    public Result<OrderLineStatAmountResponse> statAmountES(PageQueryOrderLineESListRequest pageQueryOrderLineESListRequest) {
        Result<OrderLineStatAmountResponse> result = orderLineQueryServiceRepository.statAmountES(pageQueryOrderLineESListRequest);
        return  result;
    }
}
